package com.tedu.ds.customer.service.impl;

import com.tedu.ds.base.exception.ServiceException;
import com.tedu.ds.base.response.StatusCode;
import com.tedu.ds.customer.mapper.CustomerMapper;
import com.tedu.ds.customer.pojo.dto.CustomerLoginParam;
import com.tedu.ds.customer.pojo.dto.CustomerParam;
import com.tedu.ds.customer.pojo.dto.CustomerQuery;
import com.tedu.ds.customer.pojo.entity.Customer;
import com.tedu.ds.customer.pojo.vo.CustomerVO;
import com.tedu.ds.customer.service.CustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    CustomerMapper customerMapper;

    @Override
    public void save(CustomerParam customerParam) {
        Customer customer=new Customer();
        BeanUtils.copyProperties(customerParam,customer);
        if (customer.getId()==null){
            if (customer.getRoleId()==null){
                customer.setRoleId(10);
            }
            customer.setStatus("10");
            customerMapper.reg(customer);
        }else {
            customerMapper.update(customer);
        }
    }

    @Override
    public void deleteById(Long id) {
        customerMapper.delete(id);
    }

    @Override
    public List<CustomerVO> select(CustomerQuery customerQuery) {
        List<CustomerVO> list=customerMapper.select(customerQuery);
        return list;
    }

    @Override
    public CustomerVO login(CustomerLoginParam login) {
        Customer customer=customerMapper.login(login.getUsername());
        if (customer==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }else if (login.getPassword().equals(customer.getPassword())){
            CustomerVO customerVO=new CustomerVO();
            BeanUtils.copyProperties(customer,customerVO);
            return customerVO;
        }else {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
    }

    @Override
    public CustomerVO selectByUsername(String username) {
        Customer customer=customerMapper.login(username);
        CustomerVO customerVO=new CustomerVO();
        BeanUtils.copyProperties(customer,customerVO);
        return customerVO;
    }

}

